<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Session;
use DB;
class OrderController extends Controller
{
    //
    public function add()
    {
    	$data = DB::table('cart') -> where('uid',Session('user_info')->id) -> get();
    	$num = 0;
    	foreach($data as $row)
    	{
    		$res = DB::table('goods') -> where('gid',$row -> gid) -> first();
    		$row -> gid = $res;
    		$num += ($res -> gpirce) * ($row -> bcnt);
    	} 

    	return view('home.order.add',['title'=>'订单确认','data' => $data,'num' => $num]);

    }

    public function insert(Request $request)
    {

    	$orderdetail = [];
    	// 拼order表数据
    	$orderdata['uid'] = Session('user_info') -> id;
    	$orderdata['rec'] = $request -> rec;
    	$orderdata['addr'] = $request -> addr;
    	$orderdata['tel'] = $request -> tel;
    	$orderdata['umsg'] = $request -> umsg;
    	$orderdata['status'] = 0;
    	// dd($orderdata);
    	// dd($orderdetail);
    	// 开启实物
    	DB::beginTransaction();
    	$orderdetail['oid'] = DB::table('order') -> insertGetId($orderdata);
    	if($orderdetail['oid'])
    	{
    		$data = DB::table('cart') -> where('uid',Session('user_info')->id) -> get();
    	    $info['jifen'] = 0;
	    	foreach($data as $row)
	    	{
	    		// 拼detail表数据
	    		$res = DB::table('goods') -> where('gid',$row -> gid) -> first();
	    		$row -> gid = $res;
	    		$orderdetail['gid'] = $row -> gid -> gid;
	    		$orderdetail['price'] = $row -> gid -> gpirce;
	    		$orderdetail['count'] = $row -> bcnt;
                $info['jifen'] += $orderdetail['count'] * $orderdetail['price'];
                $goods['gnum'] = ($row -> gid -> gnum) - ($row -> bcnt);
                $goods['gscl'] = ($row -> gid -> gscl) + ($row -> bcnt);
                // 更改销量 和库存
                $idgoods =  DB::table('goods') -> where('gid', $row -> gid -> gid) -> update($goods);
	    		$aa = DB::table('detail') -> insert($orderdetail);
	    		if(!$aa || !$idgoods)
	    		{
	    			DB::rollback();
	    			return back();
	    		}
	    	}
            // 添加积分

            $jifen = DB::table('users_info') -> where('user_id',Session('user_info')->id) -> first() -> jifen;
            $info['jifen'] = $info['jifen'] +$jifen;
            $isjifen = DB::table('users_info') -> where('user_id',Session('user_info')->id) -> update($info);
            if(!$isjifen)
            {
                DB::rollback();
                return back();
            }
	    	// 删除购物车内容
	    	// $goods DB::table('cart') -> where('uid',Session('user_info');
	    	$isdel =DB::table('cart') -> where('uid',Session('user_info')->id) -> delete();
	    	if($isdel)
	    	{
	    		DB::commit();
	    		return redirect('/home/user/userorder');
	    	}else
	    	{
	    		DB::rollback();
	    	}

    	}else
    	{
    		DB::rollback();
    	}
    }

    public function shouhuo($id)
    {   
        $data['status'] = 3;
        $res = DB::table('order') -> where('id', $id) -> update($data);
        if($res)
        {
     
            $data = DB::table('order') -> where('uid', Session('user_info')->id) -> orderby('id','desc') -> get();
            // dd($data);
            $detail = [];
            foreach($data as $row)
            {
                $detail[$row -> id] = DB::table('detail') -> where('oid', $row -> id) -> get();
                $detail[$row -> id] -> num = 0;
                foreach($detail[$row -> id] as $line)
                {   
                    $detail[$row -> id] ->num += ($line -> price) * ($line -> count);
                    $line -> gid = DB::table('goods') -> where('gid', $line -> gid) -> first();
                }
            }
            // dd($detail);
            return view('home.user.userorder',['title'=>'我的订单','data' => $data, 'detail' => $detail]);
    }
    }
}
